//实现秒杀的功能
function ms() {
    now = new Date()
    year = now.getFullYear()
    month = now.getMonth() + 1
    day = now.getDate()
    hour = now.getHours()
    min = now.getMinutes()
    sec = now.getSeconds()
    timeArr = [0, 10, 14, 20, 22, 24]
    endTimeArr = []
    startTimeArr = []
    $.each(timeArr, function (index, dom) {
        if (hour < dom) {
            endTimeArr.push(dom)
        } else {
            startTimeArr.push(dom)
        }
    })
    startTime = Math.max(...startTimeArr)
    endTime = Math.min(...endTimeArr)
    target = new Date(year, month, day, endTime - 1, 42, 00)
    changeTime(now, target)
    $('.round').html(startTime + ':00 场')
    if (now.getHours() == target.getHours() && now.getMinutes() == target.getMinutes() && now.getSeconds() == target.getSeconds()) {
        clearInterval(timer)
        $('.round').siblings('.desc').html('距离开始还有')
        $('.countdown').children().eq(0).html('00')
        $('.countdown').children().eq(2).html('00')
        $('.countdown').children().eq(4).html('00')
        now = new Date()
        year = now.getFullYear()
        month = now.getMonth() + 1
        day = now.getDate()
        hour = now.getHours()
        min = now.getMinutes()
        sec = now.getSeconds()
    } else {
        $('.round').siblings('.desc').html('距离结束还有')
    }
}
//更改时间并显示在页面
function changeTime(now, target) {
    diff = getTimeDiff(now, target)
    diffHour = diff.hours < 10 ? '0' + diff.hours : diff.hours
    diffMins = diff.minutes < 10 ? '0' + diff.minutes : diff.minutes
    diffSec = diff.seconds < 10 ? '0' + diff.seconds : diff.seconds
    $('.countdown').children().eq(0).html(diffHour)
    $('.countdown').children().eq(2).html(diffMins)
    $('.countdown').children().eq(4).html(diffSec)
}
//计算时间差
function getTimeDiff(time1, time2) {
    diff = Math.round(Math.abs(time1.getTime() - time2) / 1000)
    day = parseInt(diff / (60 * 60 * 24))
    hours = parseInt(diff % (60 * 60 * 24) / (60 * 60))
    minutes = parseInt(diff % (60 * 60) / 60)
    seconds = diff % 60
    return {
        day: day,
        hours: hours,
        minutes: minutes,
        seconds: seconds
    }
}